package de.westranger.forex.v2.neural;

public final class Perceptron {

	public static final double THRESHOLD = 0.5;

	public static double evaluate(final double[] weights, final double[] input) {
		double result = 0.0;
		for (int i = 0; i < weights.length; i++) {
			result += weights[i] * input[i];
		}
		return step(result);
	}

	public static double[] train(final double[][] input, final double[] output,	final double learningRate, final int epochs) {
		double[] weights = new double[input[0].length];

		for (int i = 0; i < epochs; i++) {
			for (int j = 0; j < input.length; j++) {
				double sum = 0;
				for (int k = 0; k < weights.length; k++) {
					sum += weights[k] * input[j][k];
				}
				final double diff = output[j] - step(sum);
				for (int k = 0; k < weights.length; k++) {
					weights[k] = weights[k] + (learningRate * diff * input[j][k]);
				}
			}
		}

		return weights;
	}
	
	public static double step(final double value) {
		double result = 0.0;
		if (value >= THRESHOLD) {
			result = 1.0;
		}
		return result;
	}
	


}
